package es.udc.fic.hamNeggs.web;

import org.apache.log4j.Logger;

import es.udc.fic.hamNeggs.dao.InputProject;
import es.udc.fic.hamNeggs.dao.InputUser;
import es.udc.fic.hamNeggs.manager.ProjectManager;
import es.udc.fic.hamNeggs.manager.UserManager;
import es.udc.fic.hamNeggs.manager.WorksManager;
import es.udc.fic.hamNeggs.pojo.Project;
import es.udc.fic.hamNeggs.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * Servlet implementation class Admin
 */
public class Admin extends HttpServlet {
    private static final long   serialVersionUID = 1L;
    private final static Logger LOG              = Logger.getLogger(Admin.class.getName());

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Admin() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        LOG.debug("Admin.doGet in");
        final ProjectManager prjmng = new ProjectManager();
        final UserManager usrmng = new UserManager();

        final List<Project> projects = prjmng.getProjectList();

        final List<User> users = usrmng.getUserList();

        final ArrayList<InputProject> iProjects = new ArrayList<InputProject>();

        for (final Project project : projects) {
            final InputProject iProject = new InputProject();
            iProject.setProject(project);
            iProjects.add(iProject);
        }

        request.setAttribute("projects", iProjects);

        final ArrayList<InputUser> iUsers = new ArrayList<InputUser>();

        for (final User user : users) {
            final InputUser iUser = new InputUser();
            iUser.setUser(user);
            iUsers.add(iUser);
        }

        request.setAttribute("users", iUsers);

        prjmng.close();
        usrmng.close();
        LOG.debug("Admin.doGet out");
        request.getRequestDispatcher("/admin.jsp").forward(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        LOG.debug("Admin.doPost in");
        final UserManager usrmng = new UserManager();
        final ProjectManager prjmng = new ProjectManager();
        final WorksManager wrkmng = new WorksManager();

        try {
            switch (request.getParameter("action")) {
                case "DeleteUser":
                    final User actualUserd = usrmng.getUser(new Long(request.getParameter("usrid")));
                    usrmng.deleteUser(actualUserd);
                    break;
                case "DeleteProject":
                    final Project actualProjectd = prjmng.getProject(new Long(request.getParameter("prjid")));
                    prjmng.deleteProject(actualProjectd);
                    break;
                case "ResetPassword":
                    final User userrp = usrmng.getUser(new Long(request.getParameter("userid")));
                    userrp.setPassword(org.springframework.util.DigestUtils.md5DigestAsHex(userrp.getName().getBytes()));
                    usrmng.updateUser(userrp);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            String error = "[";
            Enumeration<String> names = request.getParameterNames();
            while (names.hasMoreElements()) {
                final String paramName = names.nextElement();
                final String paramValue = request.getParameter(paramName);
                error = error.concat(paramName + ":" + paramValue + ";");
            }
            error = error.concat("]");
            LOG.error("Error while executing action: " + request.getParameter("action")
                    + "with params: " + error + "\n"
                    , e);
        }

        usrmng.close();
        prjmng.close();
        wrkmng.close();

        LOG.debug("Admin.doPost out");
        doGet(request, response);
    }

}
